package com.sgb.kjwl.view.ui.activity.transport.company;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityFleetDetailsBinding;
import com.sgb.kjwl.databinding.ActivityVehicleDetailsBinding;
import com.sgb.kjwl.model.entity.transport.AffiliatedFDetailsListEntity;
import com.sgb.kjwl.model.entity.transport.TeamRespVoEntity;
import com.sgb.kjwl.view.ui.activity.self.userInfo.VerificationCodeLoginActivity;
import com.sgb.kjwl.view.ui.activity.vehicleManagement.VehicleDetailActivity;
import com.sgb.kjwl.view.widget.CenteredImageSpan;
import com.sgb.kjwl.viewmodel.transport.FleetDetailsModel;
import com.swgk.core.dialog.RoundDialog;
import com.swgk.core.util.SharedPreferenceUtil;

/**
 * author:熊丽
 * createDate:2021/10/11 12:11
 * introduce:车队详情
 * modifier:xxx
 * modifiyDate:
 * modifiyContent:
 */
public class FleetDetailsActivity extends AppActivity {
    ActivityFleetDetailsBinding binding;
    FleetDetailsModel fleetDetailsModel;
    String fleetNo;
    private TeamRespVoEntity teamRespVoEntity;
    public static void start(Context context, String fleetNo) {
        Intent intent = new Intent();
        intent.setClass(context, FleetDetailsActivity.class);
        intent.putExtra("driverAuthNo", fleetNo);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_fleet_details);
        fleetDetailsModel = new ViewModelProvider(this).get(FleetDetailsModel.class);
        fleetNo = getIntent().getExtras().getString("driverAuthNo");
        fleetDetailsModel.gettMotorcadeDetail(fleetNo);
        binding.includeTitle.titleView.setText("车队详情");
        binding.includeTitle.ivBack.setOnClickListener(this::onMultiClick);
        binding.report.setOnClickListener(this::onMultiClick);
        binding.tvContactService.setOnClickListener(this::onMultiClick);
        binding.inviteAffiliated.setOnClickListener(this::onMultiClick);
        fleetDetailsModel.ldlist.observe(this,ldList->{
            setDetail(ldList);
        });
        fleetDetailsModel.ldadd.observe(this,success->{
            if ("success".equals(success)){
               finish();
            }
        });
    }

    @Override
    public void onMultiClick(View v) {
        switch (v.getId()) {
            case R.id.tv_contact_service:
                if (!contactsIsInSystem()) {
                    return;
                }
                if (!isLogin()) {
                    VerificationCodeLoginActivity.start(FleetDetailsActivity.this);
                    return;
                }
                if (teamRespVoEntity != null && !TextUtils.isEmpty(teamRespVoEntity.getImAccount())) {
                    buildSingleTalking(teamRespVoEntity.getImAccount());
                }
                break;
            case R.id.report:
                if (!isLogin()) {
                    VerificationCodeLoginActivity.start(FleetDetailsActivity.this);
                    return;
                }
                if (teamRespVoEntity != null && !TextUtils.isEmpty(teamRespVoEntity.getPhone())) {
                    new RoundDialog.Builder(getActivity())
                            .title("确认呼叫")
                            .content(teamRespVoEntity.getPhone())
                            .confirm("拨打")
                            .cancelable(false)
                            .confirmCallback(view -> {
                                Intent intent = new Intent(Intent.ACTION_DIAL);
                                Uri data = Uri.parse("tel:" + teamRespVoEntity.getPhone());
                                intent.setData(data);
                                getActivity().startActivity(intent);
                            })
                            .show();
                } else {
                    ToastUtil.show(this, "未获取到电话号码");
                }
                break;
            case R.id.invite_affiliated:
                    fleetDetailsModel.addOrUpdateAnchor(this,teamRespVoEntity);
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }
    private boolean contactsIsInSystem() {
        if (teamRespVoEntity == null ||
                TextUtils.isEmpty(teamRespVoEntity.getImAccount())) {
            return false;
        }
        return true;
    }
    private boolean isLogin() {
        return !"".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""));
    }
    public void setDetail(TeamRespVoEntity listEntity){
        if (listEntity!=null){
            binding.bidLayout.setVisibility(View.VISIBLE);
            this.teamRespVoEntity = listEntity;
            binding.tvFleetDetailsName.setText(listEntity.getName());
            binding.tvFleetDetailsType.setItemContent(TextUtils.isEmpty(listEntity.getBusinessType())?"/":listEntity.getBusinessType());
            binding.tvFleetDetailsBusiness.setItemContent(TextUtils.isEmpty(listEntity.getMainBusiness())?"/":listEntity.getMainBusiness());
            binding.tvFleetDetailsTeamnumber.setItemContent(listEntity.getScale()==0?"/":listEntity.getScale()+"");
            binding.tvFleetDetailsAddress.setItemContent(TextUtils.isEmpty(listEntity.getBusinessAddress())?"/":listEntity.getBusinessAddress());
            binding.tvFleetDetailsContacts.setItemContent(TextUtils.isEmpty(listEntity.getContact())?"/":listEntity.getContact());
            if (listEntity.getIsAdd()==1&&listEntity.getLinkState()!=null) {
                if (listEntity.getLinkState() == 1) {
                    binding.inviteAffiliated.setAlpha((float) 0.6);
                    binding.inviteAffiliated.setEnabled(false);
                }else if (listEntity.getLinkState()==3){
                    binding.inviteAffiliated.setAlpha((float) 0.6);
                    binding.inviteAffiliated.setEnabled(false);
                }else {
                    binding.inviteAffiliated.setAlpha(1);
                    binding.inviteAffiliated.setEnabled(true);
                }
            }else {
                binding.inviteAffiliated.setAlpha(1);
                binding.inviteAffiliated.setEnabled(true);
            }
        }else {
            binding.bidLayout.setVisibility(View.GONE);
        }
    }
}
